<style scoped>
span {
  font-size: 15px
}

.dialog-footer button:first-child {
  margin-right: 10px;
}

.el-menu-item is-active {
  color: #535bf2;
  background-color: #1a1a1a;
}


/**/
.el-button--text {
  margin-right: 15px;
}

.el-select {
  width: 300px;
}

.el-input {
  width: 300px;
}

.dialog-footer button:first-child {
  margin-right: 10px;
}


.el-button--text {
  margin-right: 15px;
}

.el-select {
  width: 300px;
}

.el-input {
  width: 300px;
}

.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>

<template>
  <div>
    <el-dialog v-model="centerDialogVisible" title="确认" width="30%" center>
      <template #default>您将删除条目:{{ fromReturn.name }}</template>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取消</el-button>
        <el-button type="danger" :disabled="BtnConfirmAble" @click="returnRow">确定</el-button>
      </span>
      </template>
    </el-dialog>

    <el-scrollbar>
      <el-table v-loading="loading" :header-cell-style="{background:'#eef1f6',color:'#606266'}" border
                :row-style="{height: '54.7px'}"
                style="font-size: 15px" stripe :data="tableData">
        <el-table-column prop="bid" v-if="false" label="图书编号"/>
        <el-table-column prop="hid" v-if="false"/>
        <el-table-column prop="bookName" label="书册名称" width="100"/>
        <el-table-column prop="name" label="借书人" width="100"/>
        <el-table-column prop="phone" label="电话" width="200"/>
        <el-table-column prop="beginTime" label="借书时间" width="200"/>
        <el-table-column prop="endTime" label="还书时间" width="200"/>
        <el-table-column  label="归还" width="120">
          <template #default="scope">
            <label> {{ scope.row.status === 0 ? '未归还' : '已归还' }}</label>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template #default="scopeEdit">

            <el-button
                v-show="scopeEdit.row.status === 0"
                link
                type="danger"
                size="small"
                @click.prevent="returnLog(scopeEdit.row.hid,
                ' 《 '+scopeEdit.row.bookName+' 》')">
              <el-icon>
                <Delete/>
              </el-icon>
              <span>还书</span>
            </el-button>
          </template>
        </el-table-column>

      </el-table>
    </el-scrollbar>

  </div>
</template>

<script lang="ts" setup>

import {onMounted, reactive, ref, watch} from "vue";
import {Delete, Edit, Operation, Search} from '@element-plus/icons-vue'

import {
  editApi,
  returnApi,
  getOneByIdApi,
   addApi, searchAllLoadApi
} from "../api/Loan";
import {ElMessage} from "element-plus";
import {ElLoading} from "element-plus/es";
import {searchTypesApi} from "../api/types";

const centerDialogVisible = ref(false)




//数据初始化
onMounted(() => {
  getTableData()
})


//分页
const page = reactive({
  current: 1,
  limit: 10,
  total: 0,
})


//分页操作


const tableData = ref([])
const loading = ref(true)
const search = reactive({
  author: "",
  bookName: "",
  tid: ""
})

function getTableData() {
  console.log(new Date())
  searchAllLoadApi().then((response: any) => {
    if (response.code === 200) {
      tableData.value = response.data.tHisresList
    } else {
      ElMessage.warning(response.msg)
    }
    loading.value = false
  }).catch(function (res) {
    loading.value = false
    ElMessage.error("页面加载失败")
  })

}


//添加修改数据初始化
const BtnConfirmAble = ref(true)


const formEdit = reactive({
  author: "",
  bid: "",
  bookName: "",
  num: 0,
  press: "",
  tid: "",
  PopupType: ""
})




//修改字段检测
watch(() => ({...formEdit}), (count, prevCount) => {
  BtnConfirmAble.value = count.author == "" ||
      count.bookName == "" ||
      count.num == 0 ||
      count.press == "" ||
      count.tid == "" ;
})




function Cancel() {
  editDialogVisible.value = false;
}


//添加系统用户
const editDialogVisible = ref(false)

const PopupType = ref(['添加', '编辑'])
const typesList = ref<any[]>([])


const fromReturn = reactive({
  id: -1,
  name: ''
})


function returnLog(id: any, name: any) {
  fromReturn.id = id
  fromReturn.name = name
  centerDialogVisible.value = true;
  BtnConfirmAble.value = false;
}

function returnRow() {
  const loading = ElLoading.service({
    lock: true,
    text: 'Loading',
    background: 'rgba(0,0,0,0.7)',
  })
  BtnConfirmAble.value = true
  returnApi(fromReturn.id)
      .then(function (response: any) {
        if (response.code === 200) {
          getTableData()
          centerDialogVisible.value = false
          ElMessage.success("还书成功")
        }
        loading.close()
      }).catch(response => {
    ElMessage.error("还书失败")
    loading.close()
  })
}

const formLabelWidth = '140px'
const inputSize = 'default'

</script>
